package com.waitingsnow.modules.shell.view
{
import com.waitingsnow.core.debug.Logger;
import com.waitingsnow.core.model.config.AppConfig;
import com.waitingsnow.core.util.ds.ExternalUtil;
import com.waitingsnow.modules.shell.model.vo.RubyServerVO;
import com.waitingsnow.modules.shell.view.baseView.ActivityStatusView;
import com.waitingsnow.modules.shell.view.baseView.CountDownView;
import com.waitingsnow.modules.shell.view.baseView.GiftView;
import com.waitingsnow.modules.shell.view.baseView.MasterBottomView;
import com.waitingsnow.modules.shell.view.baseView.PlayBottomView;
import com.waitingsnow.modules.shell.view.baseView.VideoSetView;
import com.waitingsnow.modules.shell.view.baseView.events.ActivityStatusViewEvent;
import com.waitingsnow.modules.shell.view.baseView.events.MasterBottomViewEvent;
import com.waitingsnow.modules.shell.view.controls.Alert;

import flash.display.Sprite;
import flash.events.Event;
import flash.events.FullScreenEvent;
import flash.utils.clearTimeout;
import flash.utils.setTimeout;

/**
 * 
 * @author waitingsnow
 * $Id: ShellView.as 2 2012-05-17 10:10:03Z cgslendy@gmail.com $
 */
public class ShellView extends Sprite
{
	//==========================================================================
	//  Class variables
	//==========================================================================
	static public const NAME:String = "ShellView";
	
	//==========================================================================
	//  Constructor
	//==========================================================================
	/**
	 * Constructs a new <code>ShellView</code> instance.
	 * 
	 */
	public function ShellView()
	{
		initDisplay();
	}
	
	//==========================================================================
	//  Variables
	//==========================================================================
	private var rubyServerVO:RubyServerVO;
	private var countDownView:CountDownView;
	private var activityStatusView:ActivityStatusView;
	private var masterBottomView:MasterBottomView;
	private var playBottomView:PlayBottomView;
	private var videoSetView:VideoSetView;
	private var intervalID:int;
	private var preLiveFlag:Boolean;
	private var preLiveAlert:Alert;
	private var liveAlert:Alert;
	private var giftView:GiftView;
	
	//==========================================================================
	//  Methods
	//==========================================================================
	private function initDisplay():void
	{
		ExternalUtil.callback(AppConfig.START_LIVE_TO_SWF, startLiveToSwfHandler);
		ExternalUtil.callback(AppConfig.END_LIVE_TO_SWF, endLiveToSwfHandler);
		ExternalUtil.callback(AppConfig.CHANGE_SIZE_TO_SWF, changeSizeToSwfHandler);
		ExternalUtil.callback(AppConfig.CHECK_START_LIVE_TO_SWF, checkStartLiveHandler);
		
		addEventListener(Event.ADDED_TO_STAGE, function addedToStagehandler():void
		{
			stage.addEventListener(FullScreenEvent.FULL_SCREEN, fullScreenHandler);
		});
	}
	
	private function showGift():void
	{
		if (!giftView)
		{
			giftView = new GiftView();
		}
		addChild(giftView);
	}
	
	/**
	 * 显示倒计时和剩余票
	 */	
	private function showCountDown(beginFlag:Boolean = false):void
	{
		if (!countDownView)
		{
			countDownView = new CountDownView();
		}
		var time:Number = beginFlag ? (rubyServerVO.curTime - rubyServerVO.beginTime) : 
			(rubyServerVO.beginTime - rubyServerVO.curTime);
		countDownView.setCountData(time, rubyServerVO.curTicketNum, beginFlag, rubyServerVO.hasTicket);
		countDownView.x = 110;
		countDownView.y = 50;
		addChild(countDownView);
	}
	
	private function showActivityStatus():void
	{
		activityStatusView = new ActivityStatusView();
		activityStatusView.setActivityStatus(rubyServerVO);
		activityStatusView.addEventListener(ActivityStatusViewEvent.START_LIVE_BTN_CLICK, startLiveBtnClickHandler);
		addChild(activityStatusView);
	}
	
	private function showMasterBottomView(beginFlag:Boolean = false):void
	{
		if (!masterBottomView)
		{
			masterBottomView = new MasterBottomView();
		}
		masterBottomView.y = AppConfig.swfHeight - AppConfig.bottomHeight;
		masterBottomView.setMasterData(rubyServerVO, beginFlag);
		masterBottomView.addEventListener(MasterBottomViewEvent.VIDEO_SET_CLICK_HANDLER, videoSetClickHandler);
		masterBottomView.addEventListener(MasterBottomViewEvent.START_LIVE_BTN_CLICK, startLiveBtnClickHandler);
		masterBottomView.addEventListener(MasterBottomViewEvent.END_LIVE_BTN_CLICK, endLiveBtnClickHandler);
		addChild(masterBottomView);
	}
	
	private function showPlayBottomView(begin:Boolean = false):void
	{
		if (!playBottomView)
		{
			playBottomView = new PlayBottomView();
		}
		playBottomView.y = AppConfig.swfHeight - AppConfig.bottomHeight;
		playBottomView.setGiftData(rubyServerVO);
		addChild(playBottomView);
	}
	
	public function setRubyServerData(vo:RubyServerVO):void
	{
		rubyServerVO = vo;
		Logger.log("setRubyServerData: " + vo.activityStates);
		if (vo.activityStates == 3)
		{
			showCountDown();
			showActivityStatus();
			if (vo.isMaster)
			{
				showMasterBottomView();
				var leftTime:int = vo.beginTime - 15*60*1000 - vo.curTime;
				if (leftTime > 0)
				{
					intervalID = setTimeout(checkStartLive, leftTime);
				}
			}
			else
			{
				showPlayBottomView();
			}
		}
		else if (vo.activityStates == 4)
		{
			if (vo.isMaster)
			{
				showMasterBottomView(true);
				startLiveToSwfHandler();
			}
			else
			{
				showActivityStatus();
				showPlayBottomView();
				if (!vo.hasTicket)
				{
					showCountDown(true);
				}
				else
				{
					startLiveToSwfHandler();
				}
			}
		}
		else if (vo.activityStates == 5 || vo.activityStates == 6 || vo.activityStates == 7)
		{
			showActivityStatus();
		}
		showGift();
	}
	
	private function checkStartLive():void
	{
		clearTimeout(intervalID);
		if (rubyServerVO.activityStates != 4)
		{
			Logger.log("call CHECK_START_LIVE_TO_JS");
			ExternalUtil.call(AppConfig.CHECK_START_LIVE_TO_JS);
		}
	}
	
	private function initVideoSetView():void
	{
		if (!videoSetView)
		{
			videoSetView = new VideoSetView();
			videoSetView.rubyServerVO = rubyServerVO;
		}
		addChild(videoSetView);
	}
	
	private function resize():void
	{
		if (countDownView)
		{
			countDownView.x = (AppConfig.swfWidth - countDownView.width) * 0.5;
		}
		if (activityStatusView)
		{
			activityStatusView.resize();
		}
		if (masterBottomView)
		{
			masterBottomView.y = AppConfig.swfHeight - AppConfig.bottomHeight;
			masterBottomView.resize();
		}
		if (playBottomView)
		{
			playBottomView.y = AppConfig.swfHeight - AppConfig.bottomHeight;
			playBottomView.resize();
		}
		if (videoSetView)
		{
			videoSetView.resize();
		}
	}
	
	private function showFull():void
	{
		if (playBottomView)
		{
			playBottomView.y = stage.stageHeight - AppConfig.bottomHeight;
			playBottomView.resize(true);
		}
	}
	
	private function showNormal():void
	{
		if (playBottomView)
		{
			playBottomView.y = AppConfig.swfHeight - AppConfig.bottomHeight;
			playBottomView.resize();
		}
	}
	
	//==========================================================================
	//  Event Handlers
	//==========================================================================
	private function startLiveToSwfHandler(startTime:String = null):void
	{
		Logger.log("START_LIVE_TO_SWF");
		startTime && (rubyServerVO.startTime = Date.parse(startTime));
		clearTimeout(intervalID);
		rubyServerVO.activityStates = 4;
		preLiveFlag = false;
		initVideoSetView();
		if (rubyServerVO.isMaster)
		{
			masterBottomView && masterBottomView.changeStatus();
			videoSetView && videoSetView.startPublishVideo();
			addChild(videoSetView);
			showStatus();
		}
		else
		{
			if (rubyServerVO.hasTicket)
			{
				videoSetView.startPlayVideo();
				addChild(videoSetView);
				showStatus();
			}
		}
		showGift();
	}
	
	private function showStatus(showFlag:Boolean = false):void
	{
		if (showFlag)
		{
			activityStatusView && addChild(activityStatusView);
			countDownView && addChild(countDownView);
		}
		else
		{
			activityStatusView && activityStatusView.dispose();
			countDownView && countDownView.dispose();
		}
	}
	
	private function endLiveToSwfHandler():void
	{
		Logger.log("END_LIVE_TO_SWF");
		videoSetView && videoSetView.dispose();
		playBottomView && playBottomView.dispose();
		masterBottomView && masterBottomView.dispose();
		/*videoSetView && videoSetView.showTip("异常原因，服务器已将您断开！");*/
	}
	
	/**
	 * 
	 * @param wideFlag 0 正常 1宽屏
	 * 
	 */	
	private function changeSizeToSwfHandler(wideFlag:int):void
	{
		Logger.log("CHANGE_SIZE_TO_SWF" + wideFlag);
		AppConfig.wideFlag = Boolean(wideFlag);
		AppConfig.swfWidth = wideFlag ? 918 : 592;
		AppConfig.swfHeight = wideFlag ? 559 : 376;
		resize();
	}
	
	private function videoSetClickHandler(event:MasterBottomViewEvent):void
	{
		initVideoSetView();
		videoSetView.showVideoSet();
	}
	
	private function startLiveBtnClickHandler(event:*):void
	{
		preLiveFlag = false;
		Logger.log("time: " + (rubyServerVO.curTime < (rubyServerVO.beginTime - 15*60*1000)));
		if (rubyServerVO.curTime < (rubyServerVO.beginTime - 15*60*1000))
		{
			checkStartLive();
		}
		else
		{
			ExternalUtil.call(AppConfig.START_LIVE_TO_JS);
		}
	}
	
	private function endLiveBtnClickHandler(event:MasterBottomViewEvent):void
	{
		if (preLiveFlag)
		{
			masterBottomView && masterBottomView.changeStatus(1);
			videoSetView && videoSetView.dispose();
			showStatus(true);
		}
		else
		{
			ExternalUtil.call(AppConfig.END_LIVE_TO_JS);
		}
	}
	
	private function checkStartLiveHandler(value:int):void
	{
		Logger.log("CHECK_START_LIVE_TO_SWF(" + value + ")");
		clearTimeout(intervalID);
		if (value > 0)
		{
			intervalID = setTimeout(checkStartLive, value*1000);
			Logger.log("preLiveFlag: " + preLiveFlag);
			if (!preLiveFlag)
			{
				preLiveAlert && preLiveAlert.dispose();
				preLiveAlert = new Alert("您的直播只允许提前15分钟，您现在可以试播！");
				preLiveAlert.okLabel = "试播";
				preLiveAlert.addEventListener(Event.COMPLETE, preLiveOKClickHandler);
				addChild(preLiveAlert);
			}
		}
		else
		{
			liveAlert && liveAlert.dispose();
			liveAlert = new Alert("您的直播时间已经开始，确认开始直播么！");
			liveAlert.addEventListener(Event.COMPLETE, function okHander():void
			{
				ExternalUtil.call(AppConfig.START_LIVE_TO_JS);
			});
			liveAlert.addEventListener(Event.CANCEL, function cancelHandler():void
			{
				intervalID = setTimeout(checkStartLiveHandler, 10*1000, 0);
			});
			addChild(liveAlert);
		}
	}
	
	private function preLiveOKClickHandler(event:Event):void
	{
		preLiveFlag = true;
		initVideoSetView();
		if (videoSetView)
		{
			videoSetView.startPublishVideo();
			addChild(videoSetView);
			masterBottomView && masterBottomView.changeStatus();
			showStatus();
			Logger.log("start prelive play video");
			showGift();
		}
	}
	
	private function fullScreenHandler(event:FullScreenEvent):void
	{
		if (event.fullScreen) 
		{
			showFull();
		}
		else 
		{
			showNormal();
		}
		videoSetView.tipResize(true);
	}
}
}